package demo4.mapper;

import demo4.pojo.UserInfo;
import demo4.util.DBUtil;
import demo4.util.IDBUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

@Component
public class UserMapper implements IUserMapper{
    @Autowired
    private IDBUtil dbUtil;
    @Override
    public int insert(UserInfo userInfo) {
//        System.out.println("UserMapper:插入数据");
        try (Connection connection = dbUtil.getConnection();){

            String sql = "INSERT INTO userinfo(username,password,nickname,age)" +
                    "VALUES (?,?,?,?)";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,userInfo.getUsername());
            ps.setString(2,userInfo.getPassword());
            ps.setString(3, userInfo.getNickname());
            ps.setInt(4,userInfo.getAge());
            return ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
    public UserInfo select(String username ){
        try (Connection connection = dbUtil.getConnection();){

            String sql = "SELECT id,username,password,nickname,age FROM userinfo WHERE username= ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,username);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                UserInfo userInfo = new UserInfo();
                userInfo.setId(rs.getInt("id"));
                userInfo.setUsername(rs.getString("username"));
                userInfo.setPassword(rs.getString("password"));
                userInfo.setNickname(rs.getString("nickname"));
                userInfo.setAge(rs.getInt("age"));
                return userInfo;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    @Override
    public int change(String password,String password1) {
        try( Connection connection = dbUtil.getConnection();) {

            String sql = "UPDATE userinfo SET password = ? WHERE  password = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,password1);
            ps.setString(2,password);
            int i = ps.executeUpdate();
            return i;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    public List<UserInfo> selectAll(){
        List<UserInfo> list = new ArrayList<>();
        try (Connection connection = dbUtil.getConnection()){
            String sql = "SELECT id,username,password,nickname,age" +
                    " FROM userinfo   ";
            Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery(sql);
            while (rs.next()){
                UserInfo userInfo = new UserInfo();
                userInfo.setId(rs.getInt("id"));
                userInfo.setUsername(rs.getString("username"));
                userInfo.setPassword(rs.getString("password"));
                userInfo.setNickname(rs.getString("nickname"));
                userInfo.setAge(rs.getInt("age"));
                list.add(userInfo);
            }
            return list;

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

}
